{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# gpt-4o-mini-audio-preview"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gpt-4o-mini-audio-preview\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "load_dotenv(find_dotenv(), override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "api_key=os.getenv(\"CHATFIRE_OPENAI_API_KEY\")\n",
    "base_url=os.getenv(\"CHATFIRE_OPENAI_BASE_URL\")\n",
    "# print(api_key, base_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "load_dotenv(find_dotenv(), override=True)\n",
    "import base64\n",
    "import numpy as np\n",
    "import soundfile as sf\n",
    "import time\n",
    "\n",
    "client = OpenAI(\n",
    "        api_key=api_key,\n",
    "    base_url=base_url,\n",
    "\n",
    ")\n",
    "\n",
    "start_time = time.time()\n",
    "start_audio_time = None\n",
    "completion = client.chat.completions.create(\n",
    "    model=\"gpt-4o-audio-preview\",\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
    "                {\n",
    "                    \"type\": \"input_audio\",\n",
    "                    \"input_audio\": {\n",
    "                        \"data\": \"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav\",\n",
    "                        \"format\": \"wav\",\n",
    "                    },\n",
    "                },\n",
    "                {\"type\": \"text\", \"text\": \"这段音频在说什么\"},\n",
    "            ],\n",
    "        },\n",
    "    ],\n",
    "    # 设置输出数据的模态，当前支持两种：[\"text\",\"audio\"]、[\"text\"]\n",
    "    modalities=[\"text\", \"audio\"],\n",
    "    audio={\"voice\": \"shimmer\", \"format\": \"pcm16\"},\n",
    "    # stream 必须设置为 True，否则会报错\n",
    "    stream=True,\n",
    "    stream_options={\"include_usage\": True},\n",
    ")\n",
    "\n",
    "max_tokens = 80\n",
    "outputlen = 0\n",
    "audio_string = \"\"\n",
    "\n",
    "for chunk in completion:\n",
    "    if chunk.choices:\n",
    "        if hasattr(chunk.choices[0].delta, \"audio\"):\n",
    "            try:\n",
    "                print('--',end='')\n",
    "                audio_string += chunk.choices[0].delta.audio[\"data\"]\n",
    "                if start_audio_time is None:\n",
    "                    start_audio_time = time.time()\n",
    "            except Exception as e:\n",
    "                print(chunk.choices[0].delta.audio[\"transcript\"], end=\"\")\n",
    "                outputlen += len(chunk.choices[0].delta.audio[\"transcript\"])\n",
    "                if outputlen > max_tokens:\n",
    "                    outputlen = 0\n",
    "                    print(\"\\n\")\n",
    "                    break\n",
    "    else:\n",
    "        print(chunk.usage)\n",
    "\n",
    "wav_bytes = base64.b64decode(audio_string)\n",
    "audio_np = np.frombuffer(wav_bytes, dtype=np.int16)\n",
    "sf.write(\".output/audio_assistant_py2.wav\", audio_np, samplerate=24000)\n",
    "print(f\"Total time {start_time} taken: {time.time() - start_time:.2f} seconds\")\n",
    "print(f\"Audio time {start_audio_time} taken: {time.time() - start_audio_time:.2f} seconds\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "load_dotenv(find_dotenv(), override=True)\n",
    "import base64\n",
    "import numpy as np\n",
    "import soundfile as sf\n",
    "import time\n",
    "start_time = time.time()\n",
    "start_audio_time = None\n",
    "client = OpenAI(\n",
    "    api_key=api_key,\n",
    "    base_url=base_url,\n",
    ")\n",
    "\n",
    "completion = client.chat.completions.create(\n",
    "    model=\"gpt-4o-audio-preview\",\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
    "                {\n",
    "                    \"type\": \"input_audio\",\n",
    "                    \"input_audio\": {\n",
    "                        \"data\": \"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav\",\n",
    "                        \"format\": \"wav\",\n",
    "                    },\n",
    "                },\n",
    "                {\"type\": \"text\", \"text\": \"这段音频在说什么\"},\n",
    "                # {\"type\": \"text\", \"text\": \"hello， what's your name\"},\n",
    "            ],\n",
    "        },\n",
    "    ],\n",
    "    # 设置输出数据的模态，当前支持两种：[\"text\",\"audio\"]、[\"text\"]\n",
    "    modalities=[\"text\", \"audio\"],\n",
    "    audio={\"voice\": \"shimmer\", \"format\": \"wav\"},\n",
    "    # stream 必须设置为 True，否则会报错\n",
    "    stream=True,\n",
    "    stream_options={\"include_usage\": True},\n",
    ")\n",
    "\n",
    "\n",
    "max_tokens = 80\n",
    "outputlen = 0\n",
    "audio_string = \"\"\n",
    "\n",
    "for chunk in completion:\n",
    "    if chunk.choices:\n",
    "        if hasattr(chunk.choices[0].delta, \"audio\"):\n",
    "            try:\n",
    "                print('--',end='')\n",
    "                audio_string += chunk.choices[0].delta.audio[\"data\"]\n",
    "                if start_audio_time is None:\n",
    "                    start_audio_time = time.time()\n",
    "            except Exception as e:\n",
    "                print(chunk.choices[0].delta.audio[\"transcript\"], end=\"\")\n",
    "                outputlen += len(chunk.choices[0].delta.audio[\"transcript\"])\n",
    "                if outputlen > max_tokens:\n",
    "                    outputlen = 0\n",
    "                    print(\"\\n\")\n",
    "                    break\n",
    "    else:\n",
    "        print(chunk.usage)\n",
    "\n",
    "wav_bytes = base64.b64decode(audio_string)\n",
    "audio_np = np.frombuffer(wav_bytes, dtype=np.int16)\n",
    "sf.write(\".output/audio_assistant_py.wav\", audio_np, samplerate=24000)\n",
    "print(f\"Total time {start_time} taken: {time.time() - start_time:.2f} seconds\")\n",
    "print(f\"Audio time {start_audio_time} taken: {time.time() - start_audio_time:.2f} seconds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "BadRequestError: Error code: 400 - {'error': {'message': \"Invalid value: 'Cherry'. Supported values are: 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer', 'coral', 'verse', 'ballad', 'ash', and 'sage'.  (request id: 20250525002721437676846Xhdjjazu)\", 'type': 'invalid_request_error', 'param': 'audio.voice', 'code': 'invalid_value'}}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ref\n",
    "https://learn.microsoft.com/en-us/azure/ai-services/openai/audio-completions-quickstart?tabs=keyless%2Clinux%2Ctypescript-keyless&pivots=ai-foundry-portal"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "aiv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
